﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using uTools;
public class Level4x4Love : MonoBehaviour
{
    public List<TextButton> textButtonList;
    public string targetStr = "elvo";
    public List<uTweenAlpha> tweenAlphaList;
    public GameLevelItem gameLevelItem;
    // Start is called before the first frame update
    void Start()
    {
        InitButtonEvent();
    }
    private void InitButtonEvent()
    {
        int count = textButtonList.Count;
        for (int i = 0; i < count; i++)
        {
            int tempIndex = i;
            textButtonList[i].Init("", () =>
            {
                if (GameCtrl.Instance.curGameState == GameState.Menu)
                    return;
                textButtonList[tempIndex].text.text = GetNextLetter(textButtonList[tempIndex].text.text);
                JudgeResult();
            });
        }
    }

    private string GetNextLetter(string curStr)
    {
        string nextStr = string.Empty;
        switch (curStr)
        {
            case "":
            case "e":
                nextStr = "l";
                break;
            case "l":
                nextStr = "o";
                break;
            case "o":
                nextStr = "v";
                break;
            case "v":
                nextStr = "e";
                break;
            default:
                nextStr = "l";
                break;
        }
        return nextStr;
    }

    private void JudgeResult()
    {
        int count = textButtonList.Count;
        string curStr = string.Empty;
        for (int i = 0; i < count; i++)
        {
            curStr += textButtonList[i].text.text;
        }
        if(curStr.Equals(targetStr))
        {
            gameLevelItem.TriggerResult(true);
            count = tweenAlphaList.Count;
            for(int i = 0;i < count;i++)
            {
                tweenAlphaList[i].Play(PlayDirection.Forward);
            }
        }
    }
}
